package com.example.date;

import lombok.extern.slf4j.Slf4j;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

/**
 * @author audy
 * @description: TODO
 * @date 2022/8/1 0:02
 */
@Slf4j
public class DateBug {

    public static void main(String[] args) throws ParseException {
//        dateWrong1();
        //dateRight();
        dateWrong2();
    }

    /**
     * 以字面量解析时间，输出两个时区对应的时间
     */
    public static void dateWrong1() throws ParseException {
        String stringDate = "2020-01-02 22:00:00";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Date parse1 = simpleDateFormat.parse(stringDate);
        String format1 = simpleDateFormat.format(parse1);
        log.info("parse1:{}，{},{}", parse1, format1, parse1.getTime());

        // 设置纽约时区
        simpleDateFormat.setTimeZone(TimeZone.getTimeZone("America/New_York"));
        Date parse2 = simpleDateFormat.parse(stringDate);
        String format2 = simpleDateFormat.format(parse2);
        log.info("parse2:{}，{},{}", parse2,format2, parse2.getTime());
    }

    /**
     * 对于jdk9 推出的新的日期时间类的使用
     */
    public static void dateRight(){
        //一个时间表示
        String stringDate = "2020-01-02 22:00:00";

        //初始化三个时区
        ZoneId timeZoneSH = ZoneId.of("Asia/Shanghai");
        ZoneId timeZoneNY = ZoneId.of("America/New_York");
        ZoneId timeZoneJST = ZoneOffset.ofHours(9);

        //格式化器
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        ZonedDateTime date = ZonedDateTime.of(LocalDateTime.parse(stringDate, dateTimeFormatter), timeZoneJST);

        //使用DateTimeFormatter格式化时间，可以通过withZone方法直接设置格式化使用的时区
        DateTimeFormatter outputFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss Z");
        System.out.println(timeZoneSH.getId() + outputFormat.withZone(timeZoneSH).format(date));
        System.out.println(timeZoneNY.getId() + outputFormat.withZone(timeZoneNY).format(date));
        System.out.println(timeZoneJST.getId() + outputFormat.withZone(timeZoneJST).format(date));
    }

    /**
     * 日期格式化和解析bug
     */
    public static void dateWrong2(){
        Locale.setDefault(Locale.SIMPLIFIED_CHINESE);
        System.out.println(Locale.getDefault());
        Calendar calendar = Calendar.getInstance();
        calendar.set(2019, Calendar.DECEMBER, 29,0,0,0);
        SimpleDateFormat YYYY = new SimpleDateFormat("yyyy-MM-dd");
        System.out.println("格式化: " + YYYY.format(calendar.getTime()));
        System.out.println("weekYear:" + calendar.getWeekYear());
        System.out.println("firstDayOfWeek:" + calendar.getFirstDayOfWeek());
        System.out.println("minimalDaysInFirstWeek:" + calendar.getMinimalDaysInFirstWeek());
    }
}
